﻿function success(data) {
    console.log(data);
}
$(function () {
    //获取用户组
    $("#btnGetGroup").click(function () {
        var accountName = jQuery("select option:selected").val();
        $.ajax({
            url: "/MassMessage/GetUserGroups",//?userName=" + accountName,
            type: "POST",
            data: { "accountName": accountName },
            success: function (data) {
                var json = $.parseJSON(data);
                var radioButton = "";
                $.each(json, function (i, item) {
                    //构造单选按钮

                    radioButton += "<input type='radio' name='group' value='" + item + "' />" + i;
                });
                $("table tbody tr:first td:eq(0)").html(radioButton);
            },
            error: function (data) { console.log("error:" + data); }
        });
    });

    //按用户组群发
    $("#btnSendToGroup").click(function () {
        //公众号
        var accountName = jQuery("select option:selected").val();
        //组ID
        var groupID = $('table tbody tr:first td:eq(0) input[name="group"]:checked ').val();
        //消息内容
        var msg = $("#txtContent").val();
        $.ajax({
            url: "/MassMessage/SendByGroup",//?userName=" + accountName,
            type: "POST",
            data: { "GroupTag": groupID, "accountName": accountName, "msg": msg },
            success: function (data) {
                if (data != "NO") {
                    try
                    {
                        //发送成功 需要在群发消息列表中加入消息
                        var arr1 = data.split(",");
                        //id
                        var id = arr1[0].split(":")[1];
                        //text
                        var text = arr1[1].split(":")[1];
                        var tdStr = $("table tbody tr:first td:eq(2)").html();
                        tdStr += "<br />";
                        tdStr += "<input type='radio' name='message' value='" + id + "' />" + text;
                        $("table tbody tr:first td:eq(2)").html(tdStr);
                        alert("发送成功！");
                    }
                    catch(e)
                    {
                        alert(data);
                    }
                    
                }
                else {
                    alert("按组发送失败！");
                }
            },
            error: function (data) { console.log("errorMsg:"); console.log(data); }
        });
    });

    //获取用户
    $("#btnGetUser").click(function () {
        var accountName = jQuery("select option:selected").val();
        $.ajax({
            url: "/MassMessage/GetUser",//?userName=" + accountName,
            type: "POST",
            data: { "userName": accountName },
            success: function (data) {
                var json = $.parseJSON(data);
                var radioButton = "";
                $.each(json, function (i, item) {
                    //构造单选按钮

                    radioButton += "<input type='checkbox' value='" + item + "' />" + i;
                });
                $("table tbody tr:first td:eq(1)").html(radioButton);
            },
            error: function (data) { console.log("error:" + data); }
        });
    });

    //按用户列表群发
    $("#btnSendToUsers").click(function () {
        //公众号
        var accountName = jQuery("select option:selected").val();
        //用户列表，需要拼接成以,分隔的字符串
        var userList = "";
        var i = 1;
        $.each($("table tbody tr:first td:eq(1) input"), function (i, item) {
            //获取选中的用户
            if ($(item).attr("checked")) {
                userList += "," + $(item).val();
            }
        });
        //消息内容
        var msg = $("#txtContent").val();
        $.ajax({
            url: "/MassMessage/SendToUsers",//?userName=" + accountName,
            type: "POST",
            data: { "userIdList": userList, "accountName": accountName, "msg": msg },
            success: function (data) {
                if (data != "NO") {
                    try {
                        //发送成功 需要在群发消息列表中加入消息
                        var arr1 = data.split(",");
                        //id
                        var id = arr1[0].split(":")[1];
                        //text
                        var text = arr1[1].split(":")[1];
                        var tdStr = $("table tbody tr:first td:eq(2)").html();
                        tdStr += "<br />";
                        tdStr += "<input type='radio' name='message' value='" + id + "' />" + text;
                        $("table tbody tr:first td:eq(2)").html(tdStr);
                        alert("发送成功！");
                    }
                    catch (e) {
                        alert(data);
                    }
                }
                else {
                    alert("发送失败！");
                }
            },
            error: function (data) { console.log("errorMsg:"); console.log(data); }
        });
    });

    //预览群发消息
    $("#btnPreview").click(function () {
        //公众号
        var accountName = jQuery("select option:selected").val();
        //用户列表，需要拼接成以,分隔的字符串
        var userList = "";
        var i = 1;
        $.each($("table tbody tr:first td:eq(1) input"), function (i, item) {
            //获取选中的用户
            if ($(item).attr("checked")) {
                userList += "," + $(item).val();
            }
        });
        //消息内容
        var msg = $("#txtContent").val();
        $.ajax({
            url: "/MassMessage/PreView",
            type: "POST",
            data: { "userIdList": userList, "accountName": accountName, "msg": msg },
            success: function (data) {
                alert(data + (new Date()));
            },
            error: function (data) { console.log("error:" + data); }
        });
    });

    //删除群发消息
    $("#btnDelete").click(function () {
        //公众号
        var accountName = jQuery("select option:selected").val();
        var messageIDS = $('table tbody tr:first td:eq(2) input[name="message"]:checked ').val();
        $.ajax({
            url: "/MassMessage/DeleteMessages",
            type: "POST",
            data: { "messageIDS": messageIDS, "accountName": accountName },
            success: function (data) {
                if (data == "OK") {
                    $('table tbody tr:first td:eq(2) input[name="message"]:checked ').remove();
                    alert("删除成功");
                }
                else if (data == "NO") {
                    alert("删除失败！");
                }
                else {
                    alert(data);
                }

            },
            error: function (data) { console.log("error:" + data); }
        });
    });

    //查询群发状态
    $("#btnGetStatus").click(function () {
        //公众号
        var accountName = jQuery("select option:selected").val();
        var messageid = $('table tbody tr:first td:eq(2) input[name="message"]:checked ').val();
        $.ajax({
            url: "/MassMessage/GetMessageStates",
            type: "POST",
            data: { "messageID": messageid, "accountName": accountName },
            success: function (data) {
                alert(data);
            },
            error: function (data) { console.log("error:" + data); }
        });
    });
});